package com.ruoyi.system.service.impl;

import java.util.List;

import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SecureUtil;
import com.ruoyi.system.domain.Account;
import com.ruoyi.system.domain.YyPatient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.YyDoctorMapper;
import com.ruoyi.system.domain.YyDoctor;
import com.ruoyi.system.service.IYyDoctorService;
import com.ruoyi.common.core.text.Convert;

/**
 * 医生Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-24
 */
@Service
public class YyDoctorServiceImpl implements IYyDoctorService
{
    @Autowired
    private YyDoctorMapper yyDoctorMapper;

    /**
     * 查询医生
     *
     * @param doctorId 医生主键
     * @return 医生
     */
    @Override
    public YyDoctor selectYyDoctorByDoctorId(Long doctorId)
    {
        return yyDoctorMapper.selectYyDoctorByDoctorId(doctorId);
    }

    /**
     * 查询医生列表
     *
     * @param yyDoctor 医生
     * @return 医生
     */
    @Override
    public List<YyDoctor> selectYyDoctorList(YyDoctor yyDoctor)
    {
        return yyDoctorMapper.selectYyDoctorList(yyDoctor);
    }

    /**
     * 新增医生
     *
     * @param yyDoctor 医生
     * @return 结果
     */
    @Override
    public int insertYyDoctor(YyDoctor yyDoctor)
    {
        String password = yyDoctor.getPassword();
        String md5 = SecureUtil.md5(password);
        yyDoctor.setPassword(md5);
        return yyDoctorMapper.insertYyDoctor(yyDoctor);
    }

    /**
     * 修改医生
     *
     * @param yyDoctor 医生
     * @return 结果
     */
    @Override
    public int updateYyDoctor(YyDoctor yyDoctor)
    {
        String password = yyDoctor.getPassword();
        String md5 = SecureUtil.md5(password);
        yyDoctor.setPassword(md5);
        return yyDoctorMapper.updateYyDoctor(yyDoctor);
    }

    /**
     * 批量删除医生
     *
     * @param doctorIds 需要删除的医生主键
     * @return 结果
     */
    @Override
    public int deleteYyDoctorByDoctorIds(String doctorIds)
    {
        return yyDoctorMapper.deleteYyDoctorByDoctorIds(Convert.toStrArray(doctorIds));
    }

    /**
     * 删除医生信息
     *
     * @param doctorId 医生主键
     * @return 结果
     */
    @Override
    public int deleteYyDoctorByDoctorId(Long doctorId)
    {
        return yyDoctorMapper.deleteYyDoctorByDoctorId(doctorId);
    }

    @Override
    public YyDoctor login(Account account) {
        YyDoctor yyDoctor = yyDoctorMapper.selectYyDoctorByDoctorName(account.getUsername());
        if (ObjectUtil.isNull(yyDoctor)){
            throw new RuntimeException("用户不存在");
        }
        String password = account.getPassword();
        String md5 = SecureUtil.md5(password);
        if (!md5.equals(yyDoctor.getPassword())){
            throw new RuntimeException("密码错误");
        }
        return yyDoctor;
    }
}
